﻿@using MudBlazor
@using HardHatCore.HardHatC2Client.Utilities

<div>
    @if (CommandKey?.inputType == CommandKey.InputType.Text)
    {
        <MudTextField @bind-Value="@Value" />
    }
    else if (CommandKey?.inputType == CommandKey.InputType.DropDown)
    {
        <MudSelect T="string" @bind-Value="@Value">
            @foreach (var item in CommandKey?.PreDefinedValues ?? new List<string>())
            {
                <MudSelectItem Value="@item" />
            }
        </MudSelect>
    }
    else if (CommandKey?.inputType == CommandKey.InputType.Manager)
    {
        //in this case the predefined list will be filled by a function call instead of a hardcoded list
        <MudSelect T="string" @bind-Value="@Value">
            @foreach (var item in CommandKey?.PreDefinedValues ?? new List<string>())
            {
                <MudSelectItem Value="@item" />
            }
        </MudSelect>
    }
    else if (CommandKey?.inputType == CommandKey.InputType.CheckBox)
    {
        <MudCheckBox T=bool Color="Color.Primary" CheckedChanged="(e) => UpdateCheckboxValue(e)"></MudCheckBox>
    }
    else if (CommandKey?.inputType == CommandKey.InputType.File)
    {
        <MudFileUpload T="IBrowserFile" FilesChanged="UploadFiles">
            <ButtonTemplate>
                <MudButton HtmlTag="label" Variant="Variant.Filled" Color="Color.Primary" StartIcon="@Icons.Material.Filled.CloudUpload" for="@context"> Upload Files</MudButton>
            </ButtonTemplate>
        </MudFileUpload>
    }
</div>

@code {
    [Parameter] public CommandKey CommandKey { get; set; }
    private string _value;

    [Parameter]
    public string Value
    {
        get => _value;
        set
        {
            if (_value != value)
            {
                _value = value;
                ValueChanged.InvokeAsync(value);
            }
        }
    }

    IList<IBrowserFile> files = new List<IBrowserFile>();
    [Parameter] public EventCallback<string> ValueChanged { get; set; }



    private void UpdateCheckboxValue(bool? isChecked)
    {
        Value = (bool)isChecked ? "true" : "";
        ValueChanged.InvokeAsync(Value);
    }

    private void UploadFiles(IBrowserFile file)
    {
        Value = file.Name;
    }
}
